<!DOCTYPE html>
<html lang="zh_CN">
<head>
    <#include "../common/header.ftl">
    <script src="/js/ckeditor/ckeditor.js"></script>
</head>
<body>

<div class="container " style="margin-top: 20px">
    <#include "../common/top.ftl"/>
    <div class="row">
        <div class="col-sm-3">
            <!--设置当前要回显当前菜单,必须在载人菜单前完成设置-->
            <#assign menu="strategyDetail"/>
            <#include "../common/menu.ftl"/>
        </div>
        <div class="col-sm-9">
            <div class="row">
                <div class="col-sm-12">
                    <h1 class="page-head-line">攻略文章管理</h1>
                </div>
            </div>
            <!--高级查询--->
            <form class="form-inline" id="searchForm" action="/strategyDetail/list.do" method="post">
                <input type="hidden" id="currentPage" name="currentPage" value="1">
                <div class="form-group">
                    <label>关键字:</label>
                    <input type="text" class="form-control" name="keyword" value="${(qo.keyword)!}"
                           placeholder="请输入标题">
                </div>
                <button type="submit" class="btn btn-info">
                    <span class="glyphicon glyphicon-search"></span> 查询
                </button>
                <button type="button" class="btn btn-success updateBtn">
                    <span class="glyphicon glyphicon-plus"></span> 添加攻略文章
                </button>
            </form>

            <table class="table table-striped table-hover">
                <tr>
                    <th>标题</th>
                    <th>封面</th>
                    <th>发布时间</th>
                    <th>排序</th>
                    <th>攻略类别</th>
                    <th>状态</th>
                    <th>操作</th>
                </tr>
                <#list result.list as entity>
                <tr>
                    <td>${entity.title!}</td>
                    <td><img src="${entity.coverUrl!}" width="100px"/></td>
                    <td>${(entity.releaseTime?string("yyyy-MM-dd"))!}</td>
                    <td>${(entity.sequence)!}</td>
                    <td>${(entity.catalog.name)!}</td>
                    <td>${(entity.stateName)!}</td>
                    <td><a href="javascript:;" class="updateBtn" data-json='${entity.jsonString}'>修改</a></td>
                </tr>
                </#list>
            </table>
           <#include "../common/page.ftl">
        </div>
    </div>
</div>

<div class="modal fade" id="inputModal" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">编辑/增加</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" style="margin: -3px 20px" action="/strategyDetail/saveOrUpdate.do"
                      method="post" id="editForm"
                      enctype="multipart/form-data">
                    <input type="hidden" name="id"/>
                    <input type="hidden" name="coverUrl"/>
                    <div class="form-group inline">
                        <label class="col-lg-4 control-label">标题：</label>
                        <div class="col-lg-6">
                            <input type="text" class="form-control" name="title" placeholder="请输入标题"/>
                        </div>
                    </div>
                    <div class="form-group" inline>
                        <label class="col-lg-4 control-label">封面：</label>
                        <div class="col-lg-6">
                            <img id="coverUrl" style="width: 100px"/>
                            <input type="file" class="form-control" name="image"/>
                        </div>
                    </div>
                    <div class="form-group" inline>
                        <label class="col-lg-4 control-label">所属攻略：</label>
                        <div class="col-lg-6">
                            <select type="text" class="form-control" name="strategy.id" id="strategy">
                                <option value="-1">---请选择---</option>
                                <#list strategies as s>
                                    <option value="${s.id}">${s.title}</option>
                                </#list>
                            </select>
                        </div>
                    </div>
                    <div class="form-group" inline>
                        <label class="col-lg-4 control-label">攻略类别：</label>
                        <div class="col-lg-6">
                            <select type="text" class="form-control" name="catalog.id" id="catalog">

                            </select>
                        </div>
                    </div>
                    <div class="form-group" inline>
                        <label class="col-lg-4 control-label">是否发布：</label>
                        <div class="col-lg-6">
                            <select class="form-control" name="state">
                                <option value="1">否</option>
                                <option value="2">是</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group" inline>
                        <label class="col-lg-4 control-label">排序：</label>
                        <div class="col-lg-6">
                            <input type="text" class="form-control" name="sequence"/>
                        </div>
                    </div>
                    <textarea id="content" name="content.content" cols="10" rows="10">
                      </textarea>

                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary saveBtn">保存</button>
            </div>
        </div>
    </div>
</div>


<script>


    var ck = CKEDITOR.replace('content', {
        filebrowserBrowseUrl: '/xxx',
        filebrowserUploadUrl: '/uploadImage.do'
    });


    $("#strategy").change(function () {
        var id = this.value;
        $.get("/strategyCatalog/selectCatalogByStrategyId.do", {"strategyId": id}, function (data) {
            var html = "<option value='-1'>---请选择---</option>";
            $.each(data, function (index, ele) {
                html += "<option value='" + ele.id + "'>" + ele.name + "</option>";
            })
            $("#catalog").html(html);
        }, "json")
    });

    $(".updateBtn").click(function () {
        $("#editForm").clearForm(true);
        var json = $(this).data("json");
        if (json) {
            $("#editForm input[name=id]").val(json.id);
            $("#editForm input[name=title]").val(json.title);
            $("#coverUrl").attr("src", json.coverUrl);
            $("#editForm input[name=coverUrl]").val(json.coverUrl);
            $("#editForm input[name=sequence]").val(json.sequence);
            $("#editForm select[name=state]").val(json.state);
            $("#strategy").val(json.strategyId);

            $.get("/strategyCatalog/selectCatalogByStrategyId.do", {"strategyId": json.strategyId}, function (data) {
                var html = "<option value='-1'>---请选择---</option>";
                $.each(data, function (index, ele) {
                    html += "<option value='" + ele.id + "'>" + ele.name + "</option>";
                })
                $("#catalog").html(html);
                $("#catalog").val(json.catalogId);
            }, "json")

            $.get("/strategyDetail/getContentByDetailId.do",{"detailId":json.id},function (data) {
                ck.setData(data.content);
            },"json")
        }


        $("#inputModal").modal("show");
    });

    $(".saveBtn").click(function () {
        //将CKEditor中的数据设置到文本域中
        var value = ck.getData();
        $("#content").val(value);
        $("#editForm").ajaxSubmit(function (data) {
            console.log(data);
            if (data.success) {
                reload();
            } else {
                $.message.alert("提示", data.msg);
            }
        });
    });


</script>
</body>
</html>